<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Magic Compass Alipay Barcode Merchant Site</title>
<link href="<%=request.getContextPath()%>/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="<%=request.getContextPath()%>/resources/css/style.css" rel="stylesheet">
<script src="<%=request.getContextPath()%>/resources/jquery/jquery-2.2.1.min.js"></script>
<script src="<%=request.getContextPath()%>/resources/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$('#barcodePayForm').submit(function (e) {
		e.preventDefault();
		$("#submitButton").prop('disabled', true);
		$("#waitingModal").modal({
		  backdrop: 'static',
		  keyboard: false
		});
		//alert("ok");
		send2Alipay();
	});
});
function send2Alipay(){
	$.ajax({
		url: '<%=request.getContextPath()%>/alipayBarcodePaymentController/alipayBarcodePayment',
		dataType: "json",
		contentType: "application/json",
		data: {
			customerALIPAYBarcode: $("#barcode").val(),
			merchantTransactionId: $("#merchantTransactionId").val(),
		},
		success: function(data) {
			if(data.redirect){
				window.location = data.redirectURL+"?resultCode="+data.responseResultCode+"&resultReason="+data.responseResultReason;
			}
		},
		error:function(e, x, settings, exception) {
            var message;
            var statusErrorMap = {
                '400' : "Server understood the request, but request content was invalid.",
                '401' : "Unauthorized access.",
                '403' : "Forbidden resource can't be accessed.",
                '500' : "Internal server error.",
                '503' : "Service unavailable."
            };
            if (x.status) {
                message =statusErrorMap[x.status];
                                if(!message){
                                      message="Unknown Error \n.";
                                  }
            }else if(exception=='parsererror'){
                message="Error.\nParsing JSON Request failed.";
            }else if(exception=='timeout'){
                message="Request Time out.";
            }else if(exception=='abort'){
                message="Request was aborted by the server";
            }else {
                message="Unknown Error \n.";
            }
            message = message + ", This is abnormal.Please contact support. Please use Home button to return home page.";
            //alert(message);
            $('#waitingModal').modal('hide');
            $("#warningModalContent").text(message);
			$("#warningModal").modal();
		},
		type: 'GET'
	});
}
</script>
</head>
<body>
<div id="wrap">
<%@ include file="../header.jsp" %>
<div class="container">
	<div class="page-header">
		<h1 class="text-center">Payment Confirm Page</h1>
	</div>
	<br>
	<form method="POST" action="#" id="barcodePayForm">
		<div class="row">
			<div class="col-md-2">&nbsp;</div>
			<div class="col-md-8">
				<table class="table table-bordered table-condensed table-striped">
					<tr>
				        <td class="lead">Merchant Name</td>
			            <td><%=request.getAttribute("merchantName") %></td>
			        </tr>
					<tr>
				        <td class="lead">Total Amount</td>
				        <td><%=request.getAttribute("totalAmt") %></td>
			        </tr>
			        <tr>
				        <td class="lead">Customer barcode</td>
				        <td><%=request.getAttribute("barcode") %></td>
			        </tr>
			    </table>
				<br>
				<div class="row">
					<div class="col-md-5">&nbsp;</div>
					<div class="col-md-2">
						<button type="submit" class="btn btn-primary btn-md btn-block" name="submitButton" id="submitButton">Submit</button>
						<input type="hidden" id="merchantTransactionId" name="merchantTransactionId" value="<%=request.getAttribute("merchantTransactionId") %>">
						<input type="hidden" id="barcode" name="barcode" value="<%=request.getAttribute("barcode") %>">
						<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
					</div>
					<div class="col-md-5">&nbsp;</div>
				</div>
				
			</div><!-- main content div end-->
			<div class="col-md-2">&nbsp;</div>
		</div>
	</form>

<%@ include file="../warningModalDIV.jsp" %>
<%@ include file="../waitingModalDIV.jsp" %>
	
</div><!-- container end -->
</div><!-- wrap end -->
<%@ include file="../footer.jsp" %>
</body>
</html>